package com.yanglei.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Client {

	private JFrame jframe;
	private JPanel jpanel1;
	private JPanel jpanel2;
	private JPanel jpanel3;
	private JPanel jpanel4;
	private JTextField jta1;
	private JTextArea jta2;// 聊天信息窗口
	private JTextArea jta3;// 在线用户列表

	private final DefaultListModel dlm = new DefaultListModel();

	private JList jlist;

	private JButton jbutton;
	private JButton jbutton2;
	private JScrollPane jscrollpane1;
	private JScrollPane jscrollpane2;
	private ClientConnection clientConnection;
	private singleClient singleClient;
	private String singleName = "";

	public void setSingleName(String singleName) {
		this.singleName = singleName;
	}

	public JButton getJbutton2() {
		return jbutton2;
	}

	public singleClient getSingleClient() {
		return singleClient;
	}

	public ClientConnection getClientConnection() {
		return clientConnection;
	}

	public JTextField getJta1() {
		return jta1;
	}

	public JTextArea getJta2() {
		return jta2;
	}

	public JTextArea getJta3() {
		return jta3;
	}

	public DefaultListModel getDlm() {
		return dlm;
	}

	public JFrame getJframe() {
		return jframe;
	}

	public Client(ClientConnection clientConnection) {
		jframe = new JFrame("客户端");
		this.clientConnection = clientConnection;
		init();
	}

	public void init() {
		jpanel1 = new JPanel();
		jpanel2 = new JPanel();
		jpanel3 = new JPanel();
		jpanel4 = new JPanel();

		jscrollpane1 = new JScrollPane();
		jscrollpane2 = new JScrollPane();

		jpanel2.setSize(new Dimension(67, 28));

		jta1 = new JTextField(15);
		jta2 = new JTextArea(15, 20);
		// jta3 = new JTextArea(17, 10);
		jta2.setEditable(false);
		jbutton = new JButton("发送");
		jbutton2 = new JButton("私聊");

		jbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Client.this.sendMessage(e);
			}
		});
		// 单击 私聊发送私聊请求
		jbutton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				isSingleNameNull();
			}
		});

		this.jframe.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					Client.this.clientConnection.sendMenssage("client close",
							"5");
					Client.this.clientConnection.getSingleClient().getJframe()
							.dispose();
				} catch (Exception e2) {
					// TODO: handle exception
				} finally {

				}
			}
		});

		FlowLayout flowlayout = new FlowLayout();
		BorderLayout borderlayout = new BorderLayout();
		BorderLayout borderlayout1 = new BorderLayout();
		BorderLayout borderlayout2 = new BorderLayout();

		jpanel1.setLayout(flowlayout);

		jpanel1.add(jta1);
		jpanel1.add(jbutton);

		jscrollpane1.setViewportView(jta2);
		jpanel2.add(jscrollpane1);

		jpanel3.setLayout(borderlayout1);
		jpanel3.setBorder(BorderFactory.createTitledBorder("聊天信息"));
		jpanel3.add(jpanel1, borderlayout1.SOUTH);
		jpanel3.add(jpanel2, borderlayout1.CENTER);

		jpanel4.setBorder(BorderFactory.createTitledBorder("在线列表"));
		// 显示在线列表
		jlist = new JList(dlm);
		jlist.setVisibleRowCount(15);
		jlist.setPrototypeCellValue("Index 123456789111");
		jscrollpane2.getViewport().setView(jlist);

		// 在jlist对象是添加鼠标点击监听
		jlist.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (jlist.getSelectedIndex() != -1) {
					if (e.getClickCount() == 2) {
						singleName = (String) jlist.getSelectedValue();
					}
				}
			}

		});

		// jscrollpane2.setViewportView(jta3);
		jpanel4.setLayout(borderlayout2);
		jpanel4.add(jscrollpane2, borderlayout2.CENTER);
		jpanel4.add(jbutton2, borderlayout2.SOUTH);

		jframe.setLayout(borderlayout);

		jframe.add(jpanel4, borderlayout.EAST);
		jframe.add(jpanel3, borderlayout.CENTER);

		jframe.setTitle(clientConnection.getUsername());
		jframe.setResizable(false);
		jframe.setLocation(500, 250);
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.setVisible(true);
		jframe.pack();
	}

	private void sendMessage(ActionEvent event) {
		// 用户聊天数据
		String message = this.jta1.getText();
		// 清空聊天数据
		this.jta1.setText("");

		this.jta1.requestFocus();

		this.clientConnection.sendMenssage(message, "2");
	}

	private void twoClick(Object username) {
		clientConnection.sendSingleMessage("123", "10", (String) username);
	}

	// 判断当前的singleName是否为空值
	private void isSingleNameNull() {
		if (singleName.equals("")) {
			JOptionPane.showMessageDialog(jframe, "请选择一个私聊的对象(双击选中)", "警告",
					JOptionPane.WARNING_MESSAGE);
			this.singleName = "";
		} else if (clientConnection.getClientMap().keySet()
				.contains(singleName)) {
			JOptionPane.showMessageDialog(jframe, "你们私聊ING，请选择别人私聊", "警告",
					JOptionPane.WARNING_MESSAGE);
			this.singleName = "";
		} else {
			twoClick(singleName);
		}
	}
}
